#include "led.h"
#include "stdlib.h"
#include "memory.h"
#include "user_lib.h"

static uint8_t idx;
static PWMInstance *LED[3];//三元色
static LEDInstance *bsp_led_ins[LED_MAX_NUM] = {NULL};

#define RGB_FLOW_COLOR_CHANGE_TIME  1000
#define RGB_FLOW_COLOR_LENGHT   10
//blue-> green(dark)-> red -> blue(dark) -> green(dark) -> red(dark) -> blue
//蓝 -> 绿(灭) -> 红 -> 蓝(灭) -> 绿 -> 红(灭) -> 蓝 
uint32_t RGB_flow_color[RGB_FLOW_COLOR_LENGHT + 1] = {
    0xFF0000FF, 
    0x0000FF00, 
    0xFFFF0000, 
    0xFFFFFF00,
    0x0000FFFF,
    0x000000FF, 
    0xFF00FF00,
    0xFF00FFFF, 
    0x00FFFF00,
    0x00FF0000, 
    0xFF0000FF};

LEDInstance *LEDRegister(LED_Init_Config_s *led_config)
{
    LEDInstance *led_ins = (LEDInstance *)zmalloc(sizeof(LEDInstance));
    // 剩下的值暂时都被置零
    led_ins->led_pwm = PWMRegister(&led_config->pwm_config);
    led_ins->led_switch = led_config->init_swtich;

    bsp_led_ins[idx++] = led_ins;
    return led_ins;
}
/*
    * @brief   LED初始化
    * @param   无
    * @retval  无
*/
void LEDInit()
{
    //BLUD
    PWM_Init_Config_s led_config_B= {
            .htim = &htim5,
            .channel = TIM_CHANNEL_1,
            .dutyratio = 0,
            .period = 0.001,
    };
    LED[0] = PWMRegister(&led_config_B);
    //GREEN
    PWM_Init_Config_s led_config_G = {
            .htim = &htim5,
            .channel = TIM_CHANNEL_2,
            .dutyratio = 0,
            .period = 0.001,
    };
    LED[1] = PWMRegister(&led_config_G);
    //RED
    PWM_Init_Config_s led_config_R= {
            .htim = &htim5,
            .channel = TIM_CHANNEL_3,
            .dutyratio = 0,
            .period = 0.001,
    };
    LED[2] = PWMRegister(&led_config_R);
}
/*
    * @brief led任务
    * @param 无
    * @retval 无
*/

void LEDTask()
{
    uint16_t i, j;
    float delta_alpha, delta_red, delta_green, delta_blue;
    float alpha,red,green,blue;
    uint32_t aRGB;
    while(1)
    {
    for(i = 0; i < RGB_FLOW_COLOR_LENGHT; i++)
        {
            alpha = (RGB_flow_color[i] & 0xFF000000) >> 24;
            red = ((RGB_flow_color[i] & 0x00FF0000) >> 16);
            green = ((RGB_flow_color[i] & 0x0000FF00) >> 8);
            blue = ((RGB_flow_color[i] & 0x000000FF) >> 0);

            delta_alpha = (float)((RGB_flow_color[i + 1] & 0xFF000000) >> 24) - (float)((RGB_flow_color[i] & 0xFF000000) >> 24);
            delta_red = (float)((RGB_flow_color[i + 1] & 0x00FF0000) >> 16) - (float)((RGB_flow_color[i] & 0x00FF0000) >> 16);
            delta_green = (float)((RGB_flow_color[i + 1] & 0x0000FF00) >> 8) - (float)((RGB_flow_color[i] & 0x0000FF00) >> 8);
            delta_blue = (float)((RGB_flow_color[i + 1] & 0x000000FF) >> 0) - (float)((RGB_flow_color[i] & 0x000000FF) >> 0);

            delta_alpha /= RGB_FLOW_COLOR_CHANGE_TIME;
            delta_red /= RGB_FLOW_COLOR_CHANGE_TIME;
            delta_green /= RGB_FLOW_COLOR_CHANGE_TIME;
            delta_blue /= RGB_FLOW_COLOR_CHANGE_TIME;
            for(j = 0; j < RGB_FLOW_COLOR_CHANGE_TIME; j++)
            {
                alpha += delta_alpha;
                red += delta_red;
                green += delta_green;
                blue += delta_blue;

                aRGB = ((uint32_t)(alpha)) << 24 | ((uint32_t)(red)) << 16 | ((uint32_t)(green)) << 8 | ((uint32_t)(blue)) << 0;
                LEDShow(aRGB);
                DWT_Delay(0.0005);
            }
        }
    }
}
/*
  * @brief          显示RGB
  * @param[in]      aRGB:0xaaRRGGBB,'aa' 是透明度,'RR'是红色,'GG'是绿色,'BB'是蓝色
  * @retval         none
*/
void LEDShow(uint32_t aRGB)
{
    static uint8_t alpha;
    static uint16_t red,green,blue;

    alpha = (aRGB & 0xFF000000) >> 24;
    red = ((aRGB & 0x00FF0000) >> 16) * alpha;
    green = ((aRGB & 0x0000FF00) >> 8) * alpha;
    blue = ((aRGB & 0x000000FF) >> 0) * alpha;

    // __HAL_TIM_SetCompare(&htim5, TIM_CHANNEL_1, blue);
    // __HAL_TIM_SetCompare(&htim5, TIM_CHANNEL_2, green);
    // __HAL_TIM_SetCompare(&htim5, TIM_CHANNEL_3, red);

    PWMSetDutyRatioARR(LED[0],blue);
    PWMSetDutyRatioARR(LED[1],green);
    PWMSetDutyRatioARR(LED[2],red);
}

void LEDSwitch(LEDInstance *_led, uint8_t led_switch)
{
    if (led_switch == 1)
    {
        _led->led_switch = 1;
    }
    else
    {
        _led->led_switch = 0;
        // PWMSetPeriod(_led,0);
    }
}
void LEDSet(LEDInstance *_led, uint8_t alpha, uint8_t color_value, uint8_t brightness)
{
}


// void LED_ON()
// {
//     LED_Task_Config_s * _led_config = (LED_Task_Config_s *)malloc(sizeof(LED_Task_Config_s));
//     if(_led_config == NULL)
//     return;
//     _led_config->gpio_config.GPIOx = GPIOH;
//     _led_config->gpio_config.pin_state = RESET;
//     _led_config->gpio_config.GPIO_Pin = GPIO_PIN_10;
//     GPIOSet(GPIORegister(&_led_config->gpio_config)); 
// }

// void LED_OFF()
// {
//     LED_Task_Config_s * _led_config = (LED_Task_Config_s *)malloc(sizeof(LED_Task_Config_s));
//     if(_led_config == NULL)
//     return;
//     _led_config->gpio_config.GPIOx = GPIOH;
//     _led_config->gpio_config.pin_state = RESET;
//     _led_config->gpio_config.GPIO_Pin = GPIO_PIN_10;
//     GPIOReset(GPIORegister(&_led_config->gpio_config));
// }